package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.model.CommodityDataModel;
import com.morizhang.merchanmanager.api.model.CommodityExpireModel;
import com.morizhang.merchanmanager.api.plugins.BasicWareManage;
import org.springframework.beans.factory.annotation.Autowired;

@ServiceAnnotation(name = "AddCommodityInfoService")
public class AddCommodityInfoService implements IService {

    @Autowired
    private BasicWareManage basicWareManage;

    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        JSONObject requestData = requestParams.getJSONObject("data");
        CommodityDataModel newCommodityData = JSON.parseObject(requestData.toJSONString(), CommodityDataModel.class);
        if (requestData.getString("expireDays") == null)
            basicWareManage.addCommodity(newCommodityData, null);
        else {
            CommodityExpireModel newCommodityExpire = JSON.parseObject(requestData.toJSONString(), CommodityExpireModel.class);
            basicWareManage.addCommodity(newCommodityData, newCommodityExpire);
        }
        ApiResult result = new ApiResult();
        result.setSuccess(true);
        return result;
    }
}
